import Mock from "mockjs";
//处理路径传参
import { parameteUrl } from "@/utils/query-param";
import { handleAi } from "./ai";

function ArrSet(Arr: any[], id: string): any[] {
  let obj: any = {};
  const arrays = Arr.reduce((setArr, item) => {
    obj[item[id]] ? "" : (obj[item[id]] = true && setArr.push(item));
    return setArr;
  }, []);
  return arrays;
}
/**
 * @description: min ≤ r ≤ max  随机数
 * @param {*} Min
 * @param {*} Max
 * @return {*}
 */
function RandomNumBoth(Min: any, Max: any) {
  var Range = Max - Min;
  var Rand = Math.random();
  var num = Min + Math.round(Rand * Range); //四舍五入
  return num;
}
//左中
export default [
  {
    url: "/bigscreen/countUserNum",
    type: "get",
    response: () => {
      const a = Mock.mock({
        success: true,
        data: [
          {
            value: 36,
            name: "特高风险",
          },
          {
            value: 28,
            name: "高风险",
          },
          {
            value: 20,
            name: "中风险",
          },
          {
            value: 17,
            name: "低风险",
          },
          {
            value: 12,
            name: "可接受风险",
          },
        ],
      });
      a.data.onlineNum =
        a.data.totalNum - a.data.offlineNum - a.data.lockNum - a.data.alarmNum;
      return a;
    },
  },
  {
    url: "/bigscreen/countDeviceNum",
    type: "get",
    response: () => {
      const a = Mock.mock({
        success: true,
        data: {
          alarmNum: "@integer(100, 1000)",
          offlineNum: "@integer(0, 50)",
          totalNum: 698,
        },
      });
      a.data.onlineNum = a.data.totalNum - a.data.offlineNum;
      return a;
    },
  },
  //左下
  {
    url: "/bigscreen/leftBottom1",
    type: "get",
    response: () => {
      const a = Mock.mock({
        success: true,
        data: {
          "list|20": [
            {
              name: "系统内人员资质过期清单",
              createTime: "@datetime('yyyy-MM-dd HH:mm:ss')",
              content: "已生成系统外人员资质过期清单",
            },
          ],
        },
      });
      return a;
    },
  },
  //右上
  {
    url: "/bigscreen/alarmNum",
    type: "get",
    response: () => {
      const a = Mock.mock({
        success: true,
        data: {
          dateList: [
            "2021-11",
            "2021-12",
            "2022-01",
            "2022-02",
            "2022-03",
            "2022-04",
          ],
          "numList|6": ["@integer(0, 1000)"],
          "numList2|6": ["@integer(0, 1000)"],
        },
      });
      return a;
    },
  },
  //右中
  {
    url: "/bigscreen/ranking",
    type: "get",
    response: () => {
      let newNum = Mock.mock({
        "list|10": [{ value: "@integer(50,1000)",value1: "@integer(50,1000)",value2: "@integer(50,1000)",value3: "@integer(50,1000)", name: "@city()", name2: "@cname()" }],
      }).list;
      //   console.log("ranking",num);
      // let newNum: any = []
      // num.map((item: any) => {
      //   if (newNum.length < 10) {
      //     newNum.push(item);
      //   }
      // });
      let arr = newNum.sort((a: any, b: any) => {
        return b.value - a.value;
      });
      let a = {
        success: true,
        data: arr,
      };
      return a;
    },
  },
  //右下
  {
    url: "/rightBottom2",
    type: "get",
    response: () => {
      const a = Mock.mock({
        success: true,
        data: {
          "list|40": [
            {
              alertdetail: "@csentence(5,10)",
              "alertname|1": ["水浸告警", "各种报警"],
              alertvalue: "@float(60, 200)",
              createtime: "2022-04-19 08:38:33",
              deviceid: null,
              "gatewayno|+1": 10000,
              phase: "A1",
              sbInfo: "@csentence(10,18)",
              "terminalno|+1": 100,
              provinceName: "@province()",
              cityName: "@city()",
              countyName: "@county()",
            },
          ],
        },
      });
      return a;
    },
  },
  //安装计划
  {
    url: "/bigscreen/installationPlan",
    type: "get",
    response: () => {
      let num = RandomNumBoth(12, 12);
      const a = Mock.mock({
        ["category|" + num]: ["@cname()"],
        ["val1|" + num]: ["@integer(10, 100)"],
        ["val2|" + num]: ["@integer(10, 100)"],
        ["val3|" + num]: ["@integer(10, 100)"],
        ["val4|" + num]: ["@integer(10, 100)"],
      });
      // let lineData = [],
      //   rateData = [];
      // for (let index = 0; index < num; index++) {
      //   let lineNum = Mock.mock("@integer(0, 100)") + a.barData[index];
      //   lineData.push(lineNum);
      //   let rate = a.barData[index] / lineNum;
      //   rateData.push((rate * 100).toFixed(0));
      // }
      // a.lineData = lineData;
      // a.rateData = rateData;
      return {
        success: true,
        data: a,
      };
    },
  },
  {
    url: "/bigscreen/centerMap",
    type: "get",
    response: (options: any) => {
      let params = parameteUrl(options.url);
      //不是中国的时候
      if (params.regionCode && !["china"].includes(params.regionCode)) {
        const a = Mock.mock({
          success: true,
          data: {
            "dataList|100": [
              {
                name: "@city()",
                value: "@integer(1, 1000)",
              },
            ],
            regionCode: params.regionCode, //-代表中国
          },
        });
        return a;
      } else {
        const a = Mock.mock({
          success: true,
          data: {
            "dataList|12": [
              {
                name: "@province()",
                value: "@integer(1, 1100)",
              },
            ],
            regionCode: "china",
          },
        });
        // 去重
        a.data.dataList = ArrSet(a.data.dataList, "name");
        return a;
      }
    },
  },
  {
    url: "/bigscreen/ai",
    type: "post",
    response: (options: any) => {
      const a = Mock.mock({
        success: true,
        data: handleAi(JSON.parse(options.body)),
      });
      return a;
    },
  },
];
